﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using NetSurveillance.WebApp.DataServices.Models;
using NetSurveillance.WebApp.Models;
using NetSurveillance.WebApp.Utilities;

namespace NetSurveillance.WebApp.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            var task = CurrentTask ?? DataProvider.TaskService.GetLastTask();
            var serverStatusModel = new ServerStatusModel();
            if (task == null)
            {
                serverStatusModel.TaskStatus = serverStatusModel.Case = "未有任何会话在运行";
            }
            else
            {
                var caseModel =
                    CurrentCase ?? DataProvider.CaseService.GetModelById(task.CaseId);
                serverStatusModel.Case = caseModel.Name;
                serverStatusModel.Task = task.Name;
                serverStatusModel.TaskStatus = GetTaskStatusDesc(task.TaskStatus);
                var setupWizard =
                    DataProvider.SetupWizardService.GetAllModels(false, p => p.TaskId == task.Id).FirstOrDefault();
                if (setupWizard != null)
                {
                    var lineDic =
                        new Dictionary<string, string>
                            {
                                {"ytw", Resources.ViewResource.SetupWizard_NetAccessType_YTW},
                                {"adsl", Resources.ViewResource.SetupWizard_NetAccessType_ADSL},
                                {"wifi", Resources.ViewResource.SetupWizard_NetAccessType_Wifi},
                                {"gx", Resources.ViewResource.SetupWizard_NetAccessType_GX},
                                {"panglu", Resources.ViewResource.SetupWizard_NetAccessType_PangLu},
                                {"arp", Resources.ViewResource.SetupWizard_NetAccessType_ARP},
                                {"chuanjie", Resources.ViewResource.SetupWizard_NetAccessType_ChuanJie},
                            };
                    string lineType = lineDic.ContainsKey(setupWizard.LineType)
                                          ? lineDic[setupWizard.LineType]
                                          : setupWizard.LineType;
                    string lineMethod = lineDic.ContainsKey(setupWizard.LineMethod)
                                            ? lineDic[setupWizard.LineMethod]
                                            : setupWizard.LineMethod;
                    serverStatusModel.LineDesc = string.Format("{0} ({1})", lineType, lineMethod);
                    if (setupWizard.IpSettingId.HasValue && setupWizard.IpSettingId.Value > 0)
                    {
                        var ipSetting = DataProvider.IpSettingService.GetModelById(setupWizard.IpSettingId.Value);
                        serverStatusModel.IpSetting = ipSetting;
                    }
                }
                var protocols = DataProvider.ProtocolsService.GetAllModels().ToList();
                var spProtocols =
                    SupportUtility.GetSupportProtocols().Where(p => protocols.Any(m => m.ProtocolType == p.Name));
                serverStatusModel.Protocols = string.Join("、", spProtocols.Select(p => p.DisplayText));
                serverStatusModel.Keywords = DataProvider.KeywordService.GetAllModels(false, p => p.TaskId == task.Id).ToList();
                var monitorInfo = ReplaceProtocols(DataProvider.DataRecordService.GetProtocolRecordCount(task.Id) ??
                                                   new Dictionary<string, long>(), protocols);
                serverStatusModel.MonitorInfo = monitorInfo;
                serverStatusModel.ServerStatus = DataProvider.ServerStatusService.GetLastServerStatus();
            }
            return View(serverStatusModel);
        }

        [HttpPost]
        public ActionResult GetServerStatus()
        {
            var serverStatus = DataProvider.ServerStatusService.GetLastServerStatus();
            return CamelCaseJson(new AjaxResult(serverStatus != null) { Expandos = serverStatus });
        }

        public ActionResult Help()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        private string GetTaskStatusDesc(TaskStatusType taskStatus)
        {
            string taskStatusDesc;
            switch (taskStatus)
            {
                case TaskStatusType.Running:
                    taskStatusDesc = "运行中";
                    break;
                case TaskStatusType.Suspend:
                    taskStatusDesc = "暂停";
                    break;
                case TaskStatusType.NormalEnd:
                    taskStatusDesc = "已结束";
                    break;
                default:
                    taskStatusDesc = "停止";
                    break;
            }
            return taskStatusDesc;
        }

        private IDictionary<string, long> ReplaceProtocols(IDictionary<string, long> orginalDic, IEnumerable<Protocol> protocols)
        {
            var supportProtocols = SupportUtility.GetSupportProtocols();
            if (orginalDic == null || !orginalDic.Any() || supportProtocols == null ||
                !supportProtocols.Any())
                return orginalDic;
            var replacedDic = new Dictionary<string, long>();
            foreach (var orginalProtocolInfo in orginalDic)
            {
                var supportProtocol = supportProtocols.FirstOrDefault(p => p.Name == orginalProtocolInfo.Key);
                replacedDic.Add(supportProtocol == null ? orginalProtocolInfo.Key : supportProtocol.DisplayText,
                                orginalProtocolInfo.Value);
            }
            foreach (var protocol in protocols.Where(p => !orginalDic.ContainsKey(p.ProtocolType)))
            {
                var supportProtocol = supportProtocols.FirstOrDefault(p => p.Name == protocol.ProtocolType);
                replacedDic.Add(supportProtocol == null ? protocol.ProtocolType : supportProtocol.DisplayText, 0);
            }
            return replacedDic;
        } 
    }
}
